package option;

import model.Author;
import model.GetAuthors_Optional;
import model.GetAuthors_Optional_null;

import java.lang.reflect.Field;
import java.util.Optional;
import java.util.function.Consumer;

public class demo1 {
    public static void main1(String[] args) {
       //author author = GetAuthors_Optional.getAuthor();
        //将author对象封装到Option中
        //Optional<Author> optional = Optional.ofNullable(author);
        //消费使用Optional对象封装的值
        //后面会讲(看一下就好)
        //optional.ifPresent(System.out::println);
    }

    public static void main2(String[] args) {
        //Author author= GetAuthors_Optional_null.getAuthor();
        //Optional<Author> optional = Optional.ofNullable(author);
        //optional.ifPresent(System.out::println);
    }

    public static void main3(String[] args) {
        //Optional封装的值不为null
        Optional<Author> author = GetAuthors_Optional.getAuthor();
        author.ifPresent(new Consumer<Author>() {
            @Override
            public void accept(Author author) {
                System.out.println(author.getName()+"：正在通过optional工作");
            }
        });

        //Optional封装的值为null
        Optional<Author> optional = GetAuthors_Optional_null.getAuthor();
        //optional
        optional.ifPresent(author3-> System.out.println(author3.getName()));

    }

    public  static void swap1(Integer a,Integer b) throws IllegalAccessException, NoSuchFieldException {
        Field field = Integer.class.getDeclaredField("value");

        field.setAccessible(true);

        Integer temp = new Integer(a.intValue());

        field.set(a, b.intValue());

        field.set(b, temp);

    }

    public static void swap(Integer a, Integer b) throws NoSuchFieldException, IllegalAccessException {
        Field field = Integer.class.getDeclaredField("value");
        field.setAccessible(true);
        Integer temp = new Integer(a.intValue());
        field.set(a, b.intValue());
        field.set(b, temp);


    }

    public static void main4(String[] args) throws NoSuchFieldException, IllegalAccessException {
       int[] a1 = new int[1];
       int[] b1 = new int[1];
        Integer a=1;
       Integer b=2;
        System.out.println(a+" "+b);
       swap(a,b);
        System.out.println(a+" "+b);


    }
}
